<?php
class TournamentsController extends AppController {

	var $name = 'Tournaments';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Tournament->recursive = 0;
		$this->set('tournaments', $this->paginate());//'Tournament', array('Tournament.type'=>array('OFC','NAC'))
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Tournament.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('tournament', $this->Tournament->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Tournament->create();
			if ($this->Tournament->save($this->data)) {
				$this->Session->setFlash(__('The Tournament has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Tournament could not be saved. Please, try again.', true));
			}
		}
		$events = $this->Tournament->Event->find('list');
		$this->set(compact('events'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Tournament', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tournament->save($this->data)) {
				$this->Session->setFlash(__('The Tournament has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Tournament could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tournament->read(null, $id);
		}
		$events = $this->Tournament->Event->find('list');
		$this->set(compact('events'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Tournament', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tournament->del($id)) {
			$this->Session->setFlash(__('Tournament deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
	
	function users($id){
		if (!$id) {
			$this->Session->setFlash(__('Torneo Invalido', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('userTournaments', $this->Tournament->UserTournament->find('all',array(
															'conditions' => array('UserTournament.tournament_id' => $id), //array of conditions
															//'recursive' => -1, //int
															//'fields' => array('Model.field1', 'Model.field2'), //array of field names
															//'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
															//'group' => array('Model.field'), //fields to GROUP BY
															//'limit' => n, //int
															//'page' => n, //int
															//'callbacks' => true //other possible values are false, 'before', 'after'
														)));
	}

}
?>